package com.spa.modules.system.controller;

import javax.servlet.http.HttpServletRequest;

import com.spa.common.api.vo.Result;
import com.spa.common.aspect.annotation.AutoLog;
import com.spa.modules.system.entity.SpaUserRole;

import com.spa.modules.system.service.SpaUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 描述: 角色
 * author: P0081447
 * date: 2022-06-27 17:25:43
 */
@Api(tags="角色")
@RestController
@RequestMapping(value="/sys/spaUserRole")
public class SpaUserRoleController {

    @Autowired
	private SpaUserRoleService spaUserRoleService;
	
    
    /**
     * 刷新角色权限缓存
     * 
     * @param spaUserRole
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "角色-列表")
    @GetMapping(value = "/refreshCache")
    public Result<?> refreshCache() {
        spaUserRoleService.setPermissionListToRedis();
        return Result.resultOK();
    }
    
	/**
	 * 分页列表查询
	 * 
	 * @param spaUserRole
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@AutoLog(value = "角色-列表")
	@GetMapping(value = "/listByPage")
	public Result<?> queryPageList(HttpServletRequest req, @RequestParam(name = "page", defaultValue = "1") Integer page,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		return spaUserRoleService.queryPageList(req, page, pageSize);
	}
	
	/**
     * 获取全部角色列表
     * 
     * @param spaUserRole
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "角色-列表")
    @GetMapping(value = "/allRoleList")
    public Result<?> getAllRoleList() {
        return spaUserRoleService.getAllRoleList();
    }
	
     
    /**
	 * 添加
	 *
	 * @param spaUserRole
	 * @return
	 */
	@AutoLog(value = "角色-添加")
	@ApiOperation(value="角色-添加", notes="角色-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SpaUserRole spaUserRole) {
		return spaUserRoleService.insert(spaUserRole);
	}
	
	/**
	 * 编辑
	 *
	 * @param spaUserRole
	 * @return
	 */
	@AutoLog(value = "角色-编辑")
	@ApiOperation(value="角色-编辑", notes="角色-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SpaUserRole spaUserRole) {
		return spaUserRoleService.saveById(spaUserRole);
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "角色-通过id删除")
	@ApiOperation(value="角色-通过id删除", notes="角色-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
		return spaUserRoleService.deleteById(id);
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "角色-通过id查询")
	@ApiOperation(value="角色-通过id查询", notes="角色-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) Long id) {
		return spaUserRoleService.selectById(id);
	}
}
